﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace DiaryPlanner
{
    public partial class Planner : UserControl
    {
        // Sets a DateTime to April 3, 2002 of the Gregorian calendar.
        DateTime myDT = DateTime.Now;

        // Uses the default calendar of the InvariantCulture.
        Calendar myCal = CultureInfo.InvariantCulture.Calendar;

        List<Label> monthList = new List<Label>();

        public Planner()
        {
            InitializeComponent();
            //getLastDay();
            //getFirstWeek();
            initMonth();
        }

        public void DisplayValues(Calendar myCal, DateTime myDT)
        {
            Console.WriteLine("   Era:          {0}", myCal.GetEra(myDT));
            Console.WriteLine("   Year:         {0}", myCal.GetYear(myDT));
            Console.WriteLine("   Month:        {0}", myCal.GetMonth(myDT));
            Console.WriteLine("   DayOfYear:    {0}", myCal.GetDayOfYear(myDT));
            Console.WriteLine("   DayOfMonth:   {0}", myCal.GetDayOfMonth(myDT));
            Console.WriteLine("   DayOfWeek:    {0}", myCal.GetDayOfWeek(myDT));
            Console.WriteLine("   Hour:         {0}", myCal.GetHour(myDT));
            Console.WriteLine("   Minute:       {0}", myCal.GetMinute(myDT));
            Console.WriteLine("   Second:       {0}", myCal.GetSecond(myDT));
            Console.WriteLine("   Milliseconds: {0}", myCal.GetMilliseconds(myDT));
            Console.WriteLine();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {  
            // Displays the values of the DateTime.
            DisplayValues(myCal, myDT);
            // Adds 5 to every component of the DateTime.
            myDT = myCal.AddYears(myDT, 5);
            myDT = myCal.AddMonths(myDT, 5);
            myDT = myCal.AddWeeks(myDT, 5);
            myDT = myCal.AddDays(myDT, 5);
            myDT = myCal.AddHours(myDT, 5);
            myDT = myCal.AddMinutes(myDT, 5);
            myDT = myCal.AddSeconds(myDT, 5);
            myDT = myCal.AddMilliseconds(myDT, 5);

            // Displays the values of the DateTime.
            Console.WriteLine("After adding 5 to each component of the DateTime:");
            DisplayValues(myCal, myDT);
        }

        private void initMonth()
        {
            // 모든 월들을 MonthList에 저장
            monthList.Add(null);
            monthList.Add(monthL1);
            monthList.Add(monthL2);
            monthList.Add(monthL3);
            monthList.Add(monthL4);
            monthList.Add(monthL5);
            monthList.Add(monthL6);
            monthList.Add(monthL7);
            monthList.Add(monthL8);
            monthList.Add(monthL9);
            monthList.Add(monthL10);
            monthList.Add(monthL11);
            monthList.Add(monthL12);
            monthList.Add(monthL13);
            monthList.Add(monthL14);
            monthList.Add(monthL15);
            monthList.Add(monthL16);
            monthList.Add(monthL17);
            monthList.Add(monthL18);
            monthList.Add(monthL19);
            monthList.Add(monthL20);
            monthList.Add(monthL21);
            monthList.Add(monthL22);
            monthList.Add(monthL23);
            monthList.Add(monthL24);
            monthList.Add(monthL25);
            monthList.Add(monthL26);
            monthList.Add(monthL27);
            monthList.Add(monthL28);
            monthList.Add(monthL29);
            monthList.Add(monthL30);
            monthList.Add(monthL31);
            monthList.Add(monthL32);
            monthList.Add(monthL33);
            monthList.Add(monthL34);
            monthList.Add(monthL35);
            monthList.Add(monthL36);
            monthList.Add(monthL37);
            monthList.Add(monthL38);
            monthList.Add(monthL39);
            monthList.Add(monthL40);
            monthList.Add(monthL41);
            monthList.Add(monthL42);

            //클릭 이벤트 핸들러 등록
            for(int i = 1 ; i < monthList.Count ; i++)
                monthList[i].Click += new System.EventHandler(this.month_Click);

            //해당 월에 일값을 설정
            viewMonth();
        }

        private void viewMonth()
        {
            for (int i = 1; i < monthList.Count; i++)
                monthList[i].Text = "";

            int lastDay = getLastDay();
            int firstweek = getFirstWeek();

            for (int i = 1; i <= lastDay; i++)
                monthList[i + firstweek].Text = i.ToString();
        }

        private int getLastDay()
        {
            int lastDay, tempMonth;
            DateTime tempDT = myDT;

            tempMonth = tempDT.Month;

            for (int i = 0; ; i++)
            {
                tempDT = myCal.AddDays(tempDT, 1);
                if (tempMonth != tempDT.Month)
                    break;
            }
            tempDT = myCal.AddDays(tempDT, -1);
            lastDay = tempDT.Day;

            return lastDay;
        }

        private int getFirstWeek()
        {
            int firstWeek, tempMonth;
            DateTime tempDT = myDT;

            tempMonth = tempDT.Month;

            for (int i = 0; ; i++)
            {
                tempDT = myCal.AddDays(tempDT, -1);
                if (tempMonth != tempDT.Month)
                    break;
            }
            tempDT = myCal.AddDays(tempDT, +1);

            firstWeek = (int)tempDT.DayOfWeek;

            return firstWeek;
        }

        private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
        {
            myDT = monthCalendar1.SelectionStart;
            viewMonth();
        }

        private void month_Click(object sender, EventArgs e)
        {
            for(int i = 1 ; i < monthList.Count ; i++)
                monthList[i].BackColor = System.Drawing.Color.White;

            Label label = (Label)sender;
            label.BackColor = System.Drawing.Color.Blue;
        }
    }
}
